package udpMVM;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        DatagramSocket datagramSocket = null;
        try {
            //创建客户端
            datagramSocket = new DatagramSocket(7777);
            System.out.println("欢迎光临本系统，退出请输入exit。");
            while (true) {
                //创建发送的消息
//                byte[] bytes = "测试客户端发送消息".getBytes();
                //用户输入发送消息
                System.out.println("----请输入发送给服务端消息----");
                String s = new Scanner(System.in).nextLine();

                if (s.equals("exit")) {
                    System.out.println("欢迎下次光临，退出成功！");
                    break;
                }

                byte[] bytes = s.getBytes();
                //封装数据包
                DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length, InetAddress.getLocalHost(),8081);

                //发送数据
                datagramSocket.send(datagramPacket);
            }
//            System.out.println("数据发送结束---");
        }catch (Exception e){

        }finally {
            datagramSocket.close();
        }
    }
}
